package kr.hkit.framwordk.dao;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.xml.bind.DataBindingException;

public class LoadPropertyDAO {
	private static final Properties PROPERTIES = new Properties();
	private static final String PROPERTIES_FILE = "dao.property";
	
	public static String DRIVER;
	public static String URL;
	public static String USER;
	public static String PASSWORD;
	
	private static LoadPropertyDAO instance = new LoadPropertyDAO();
	
	private LoadPropertyDAO(){
		try {
			InputStream is = new BufferedInputStream(new FileInputStream(new File(PROPERTIES_FILE)));
			PROPERTIES.load(is);
			
			DRIVER = PROPERTIES.getProperty("DRIVER");
			URL = PROPERTIES.getProperty("URL")+PROPERTIES.getProperty("DATABASE");
			USER = PROPERTIES.getProperty("USER");
			PASSWORD = PROPERTIES.getProperty("PASSWORD");
			System.out.println("aaaa");
			Class.forName(DRIVER);
		} catch (FileNotFoundException e) {
			throw new DAOConfigurationException("driver class "+DRIVER+"is missing in classping",e);
		} catch (IOException e) {
			throw new DAOConfigurationException("property file"+PROPERTIES_FILE+"is not found",e);
		} catch (ClassNotFoundException e) {
			throw new DAOConfigurationException("cannot load to property file"+PROPERTIES_FILE,e);
		}
	}
	
	private Connection createConnection() throws DAOConfigurationException{
		Connection connection = null;
		try {
			connection = DriverManager.getConnection(URL, USER, PASSWORD);
			System.out.println("bbbb");
		} catch (SQLException e) {
			throw new DAOConfigurationException("ERROR: unable to connect to database",e);
		}
		
		return connection;
		
	}
	
	public static Connection getConnection() throws DAOConfigurationException{
		return instance.createConnection();
	}
}
